package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.domain.utils.BaseAction;
import cn.itcast.bos.service.base.Fixed_AreaService;
import cn.itcast.crm.domain.Customer;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
@Actions
public class Fixed_AreaAction extends BaseAction<FixedArea> {

	@Autowired
	private Fixed_AreaService fixedArea;

	@Action(value = "fixed_save", results = {
			@Result(name = "success", location = "/pages/base/fixed_area.html", type = "redirect") })
	public String Fixed_AreaSave() {
		fixedArea.save(model);
		return SUCCESS;
	}

	@Action(value = "pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery_Fixed_Area() {

		Pageable pageable = new PageRequest(page - 1, rows);

		Specification<FixedArea> spec = new Specification<FixedArea>() {
			/***
			 * root:用于获得属性字段 CriteriaQuery:可以用于简单条件查询 CriteriaBuilder:用于构造复杂条件查询
			 */
			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> pred = new ArrayList<Predicate>();
				// 判断条件是否为空

				if (StringUtils.isNotBlank(model.getId())) {
					Predicate p1 = cb.equal(root.get("id").as(String.class), model.getId());
					pred.add(p1);
				}

				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p2 = cb.like(root.get("company").as(String.class), "%" + model.getCompany() + "%");
					pred.add(p2);
				}

				return cb.and(pred.toArray(new Predicate[0]));
			}
		};
		Page<FixedArea> pageData = fixedArea.findAll(spec, pageable);
		pushPageDataToValueStack(pageData);

		return SUCCESS;
	}

	private String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}

	@Action(value = "fixedArea_delBatch", results = {
			@Result(name = "success", type = "redirect", location = "pages/base/fixed_area.html") })
	public String delBtach() {
		String[] split = ids.split(",");
		fixedArea.delBtach(split);
		return SUCCESS;

	}

	/***
	 * 查询未关联客户列表
	 */
	@Action(value = "fixedArea_findNoAssociationCustomers", results = { @Result(name = "success", type = "json") })
	public String findNoAssoiationCustomers() {
		// 使用webClient调用webService接口
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/noAssociationCustomers")
				.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);

		ServletActionContext.getContext().getValueStack().push(collection);

		return SUCCESS;
	}

	/***
	 * 查询已关联客户列表
	 */
	@Action(value = "fixedArea_findHasAssociationFixedAreaCustomers", results = {
			@Result(name = "success", type = "json") })
	public String findHasAssoiationFixedAreaCustomer() {
		// 使用webClient调用webService接口
		// System.out.println("======" + model.getId());
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/associationfixedareacustomers/"
						+ model.getId())
				.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
				

		// System.out.println(collection);
		ServletActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}

	/***
	 * 关联客户到定区
	 */

	private String[] customerIds;

	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	@Action(value = "decidedzone_assigncustomerstodecidedzone", results = {
			@Result(name = "success", type = "redirect", location = "pages/base/fixed_area.html") })
	public String associationCustomersToFixedArea() {
		// 处理客户ID
		String customerIds = StringUtils.join(this.customerIds, ",");
		// 判断如果客户ID是NULL 那么就返回一个空字符串
		customerIds = customerIds == null ? "" : customerIds;
		WebClient.create("http://localhost:9002")
		.path("crm_management")
		.path("services")
		.path("customerService")
		.path("associationcustomerstofixedarea")
		.query("customerIds", customerIds)
		.query("fixedAreaId", this.getModel().getId()).put(null);
		return SUCCESS;

	}

	/***
	 * 定区关联快递员 快递员关联收派时间
	 */
	private Integer takeTimeId;
	private Integer courierId;

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	@Action(value = "fixedArea_associationCourierToFixedArea", results = {
			@Result(name = "success", type = "redirect", location = "pages/base/fixed_area.html") })
	public String associactionCourierToFiexedArea() {

		fixedArea.associactionCourierToFiexedArea(model, takeTimeId, courierId);

		return SUCCESS;

	}

}
